package org.example.controller;

import java.time.LocalDate;
import java.util.List;

import javax.servlet.annotation.MultipartConfig;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.oss.service.OssTemplate;

import cn.hutool.core.date.LocalDateTimeUtil;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

/**
 * @author : guorf
 * @version : v1.0
 * @description : TODO
 * @createTime : 2023/4/28 16:26
 */
@Slf4j
@RestController
@RequestMapping("/oss")
@RequiredArgsConstructor
public class OssController {

    private final OssTemplate ossTemplate;

    /**
     * bucket 名称
     */
    @Value("${oss.bucket-name}")
    private String bucketName;

    /**
     * @description TODO 创建bucket
     * @param bucketName butket名称
     **/
    @GetMapping("/create/{bucketName}")
    public void createBucket(@PathVariable String bucketName){
        ossTemplate.createBucket(bucketName);
    }

    /**
     * @description TODO 获取列表
     * @return java.util.List<com.amazonaws.services.s3.model.Bucket>
     **/
    @GetMapping("/bucket/list")
    public List<Bucket> getBucket(){
        return ossTemplate.getAllBuckets();
    }

    /**
     * @description TODO 根据bucket名称及对象名称获取对象
     * @param objectName 对象名称
     * @return com.amazonaws.services.s3.model.S3Object
     **/
    @GetMapping("/object")
    public S3Object getObject(String objectName){
        return ossTemplate.getObject(bucketName, objectName);
    }

    /**
     * @description TODO 获取文件url
     * @param objectName 对象名
     * @return java.lang.String
     **/
    @GetMapping("/object/url")
    public String getObjectURL(String objectName){
        return ossTemplate.getObjectURL(bucketName, objectName, 2);
    }

    /**
     * 通过bucketName和objectName删除对象
     * @param objectName
     * AmazonS3：https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteObject.html
     */
    @DeleteMapping("/object")
    public void deleteObject(String objectName) throws Exception {
        ossTemplate.removeObject(bucketName, objectName);
    }

    /**
     * 根据bucketName和prefix获取对象集合
     * @param prefix 前缀
     * @return
     * AmazonS3：https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjects.html
     */
    @GetMapping("/list")
    public List<S3ObjectSummary> getAllObjectsByPrefix(String prefix) {
        return ossTemplate.getAllObjectsByPrefix(bucketName, prefix, true);
    }


    /**
     * @description TODO 上传附件
     * @param multipartFile 附件
     **/
    @SneakyThrows
    @PostMapping("/upload")
    public void uploadObject(MultipartFile multipartFile) {
        String objectName = "test/".concat(LocalDateTimeUtil.format(LocalDate.now(), "yyyyMMdd")).concat("/").concat(multipartFile.getOriginalFilename());
        ossTemplate.putObject(bucketName, objectName, multipartFile.getInputStream());
    }
}
